package walk;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.dbutils.QueryRunner;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import comm.DbUtils;
import comm.LOG;

public class chc extends Thread {
	QueryRunner qr = null;

	public chc() throws Exception {
		qr = DbUtils.getQueryRunner();
	}

	public void handle(Document doc) {

		Set<String> ids = new HashSet<String>();
		Elements es = doc.select(".u");
		for (Element e : es) {
			String href = e.absUrl("href");
			ids.add(href.replace("http://my.hupu.com/", ""));
		}

		for (String id : ids) {
			try {
				qr.update("insert into hid values ( ? ) ", id);
			} catch (SQLException e1) {
				System.err.println(e1.getMessage());
			}
		}
	}

	public void handle(String page) {

		try {
			Connection c = Jsoup.connect(page);
			c.userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0");
			c.referrer("http://my.hupu.com/public/nba");
			Document pageDoc = c.get();
			handle(pageDoc);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		int start = 14;
		while (true) {

			String id = String.valueOf(start++);
			String url = "http://my.hupu.com/group/football_rumors/members-" + id;
			
			System.out.println(url);

			try {
				Connection c = Jsoup.connect(url);

				Set<String> pages = new HashSet<String>();

				c.userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0");
				c.referrer("http://my.hupu.com/public/nba");
				Document doc = c.get();

 
				handle(doc);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}

	public static void main(String[] args) throws Exception {
		new chc().start();

	}
}
